package com.pet.service.impl;

import com.pet.mapper.SysLargeDogMapper;
import com.pet.pojo.SysLargeDog;
import com.pet.pojo.Result;
import com.pet.service.ISysLargeDogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysLargeDogServiceImpl implements ISysLargeDogService {

    @Autowired
    private SysLargeDogMapper largeDogMapper;

    @Override
    public Result findByCondition(Map<String, Object> params) {
        // 获取总数
        int total = largeDogMapper.countByCondition(params);
        if (total == 0) {
            return new Result("200", "暂无数据", new HashMap<String, Object>() {
                {
                    put("total", 0);
                    put("list", List.of());
                }
            });
        }

        // 获取列表
        List<SysLargeDog> dogs = largeDogMapper.findByCondition(params);

        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", dogs);

        return new Result("200", "获取成功", result);
    }

    @Override
    public Result findById(Integer id) {
        SysLargeDog dog = largeDogMapper.findById(id);
        return dog != null ? new Result("200", "获取成功", dog) : new Result("404", "大型犬不存在");
    }

    @Override
    @Transactional
    public Result add(SysLargeDog dog) {
        try {
            int rows = largeDogMapper.insert(dog);
            return rows > 0 ? new Result("200", "添加成功") : new Result("500", "添加失败");
        } catch (Exception e) {
            return new Result("500", "添加失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result update(SysLargeDog dog) {
        try {
            int rows = largeDogMapper.update(dog);
            return rows > 0 ? new Result("200", "更新成功") : new Result("500", "更新失败，大型犬可能不存在");
        } catch (Exception e) {
            return new Result("500", "更新失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result delete(Integer id) {
        try {
            int rows = largeDogMapper.delete(id);
            return rows > 0 ? new Result("200", "删除成功") : new Result("500", "删除失败，大型犬可能不存在");
        } catch (Exception e) {
            return new Result("500", "删除失败：" + e.getMessage());
        }
    }
} 